package xyz.lovegu.server.sys.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import xyz.lovegu.server.sys.common.DataGridView;
import xyz.lovegu.server.sys.common.ResultObj;
import xyz.lovegu.server.sys.dos.Notice;
import xyz.lovegu.server.sys.mapper.NoticeMapper;
import xyz.lovegu.server.sys.service.NoticeService;
import xyz.lovegu.server.sys.vos.NoticeVo;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;

/**
 * @author 老顾
 * @title: NoticeServiceImpl
 * @projectName campus-prevention-system
 * @email: 1437594522@qq.com
 * @date 2022/5/8 23:26
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {


    /**
     * 增加公告
     *
     * @param noticeVo
     * @return
     */
    @Override
    public ResultObj addNotice(NoticeVo noticeVo) {
        try {
            Notice notice = new Notice();
            BeanUtils.copyProperties(noticeVo,notice);
            this.save(notice);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 删除单个公告
     *
     * @param noticeVo
     * @return
     */
    @Override
    public ResultObj deleteNoticeById(NoticeVo noticeVo) {
        //查询公告是否存在
        Notice notice = this.getById(noticeVo.getId());
        if (notice==null) {
            return ResultObj.NOTICE_NOT_EXIST;
        }
        try {
            this.removeById(noticeVo.getId());
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除多个公告
     *
     * @param noticeVo
     * @return
     */
    @Override
    public ResultObj deleteBatchNoticeByIds(NoticeVo noticeVo) {
        try {
            Collection<Serializable> idList = new ArrayList<>(Arrays.asList(noticeVo.getIds()));
            this.removeByIds(idList);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 修改公告
     *
     * @param noticeVo
     * @return
     */
    @Override
    public ResultObj updateNotice(NoticeVo noticeVo) {
        //查询公告是否存在
        Notice notice =this.getById(noticeVo.getId());;
        if(notice==null) {
            return ResultObj.NOTICE_NOT_EXIST;
        }
        try {
            notice.setTitle(noticeVo.getTitle());
            notice.setContent(noticeVo.getContent());
            notice.setOperatePersonName(noticeVo.getOperatePersonName());
            notice.setUpdateTime(new Date());
            this.updateById(notice);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e)
        {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 根据id查询公告
     *
     * @param id
     * @return
     */
    @Override
    public Object getNoticeById(Integer id) {
        Notice notice = this.getById(id);
        if (notice==null) {
            return ResultObj.NOTICE_NOT_EXIST;
        }else {
            return new DataGridView(notice);
        }
    }

    /**
     * 查询所有通知
     *
     * @param noticeVo
     * @return
     */
    @Override
    public Object getAllNotices(NoticeVo noticeVo) {
        IPage<Notice> page = new Page<Notice>(noticeVo.getPage(),noticeVo.getLimit());
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<Notice>();
        //进行模糊查询
        lambdaQueryWrapper.like(StringUtils.isNotBlank(noticeVo.getTitle()),Notice::getTitle,noticeVo.getTitle());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(noticeVo.getOperatePersonName()),Notice::getOperatePersonName,noticeVo.getOperatePersonName());
        //公告创建时间应大于搜索开始时间小于搜索结束时间
        lambdaQueryWrapper.eq(noticeVo.getStartTime()!=null,Notice::getCreateTime,noticeVo.getStartTime());
        lambdaQueryWrapper.eq(noticeVo.getEndTime()!=null,Notice::getCreateTime,noticeVo.getEndTime());
        //根据公告创建时间排序
        lambdaQueryWrapper.orderByDesc(Notice::getCreateTime);
        this.page(page,lambdaQueryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }
}
